#include "stdafx.h"
#include <Xmemdc.h>
#include <DrawFunc.h>
#include "XTitlebar.h"

CXTitlebar::CXTitlebar(void) : m_pIconPng(NULL)
{
	for (int i = 0; i < DEFAULT_BUTTON_COUNT; ++i)
	{
		m_pButtons[i] = NULL;
	}
	m_RectOffset.SetRectEmpty();
}

CXTitlebar::~CXTitlebar(void)
{
	for (int i = 0; i < DEFAULT_BUTTON_COUNT; ++i)
	{
		if (m_pButtons[i] != NULL)
		{
			delete m_pButtons[i];
			m_pButtons[i] = NULL;
		}
	}

	if (m_pIconPng != NULL)
	{
		DeletePngObject(m_pIconPng);
		m_pIconPng = NULL;
	}
}

/**************  command   ******************/
LRESULT CXTitlebar::OnEraseBkgnd(HDC)
{
	return FALSE;
}
LRESULT CXTitlebar::OnPaint(HDC hdc)
{
	CPaintDC dc(m_hWnd);
	CRect rect;
	GetClientRect(&rect);

	CXMemDC memDC(dc.m_hDC, rect);
	//	::PatBlt(memDC.m_hDC, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, WHITENESS);

	CWindow parentWnd = GetParent();
	CRect parentRect(rect);
	ClientToScreen(parentRect);
	parentWnd.ScreenToClient(&parentRect);

	HDC parentDC = ::GetDC(parentWnd.m_hWnd);	
	::BitBlt(memDC.m_hDC, 0, 0,  parentRect.Width(), parentRect.Height(), parentDC, parentRect.left, parentRect.top, SRCCOPY);
	ReleaseDC(parentDC);

	if (m_pIconPng != NULL)
	{
		m_pIconPng->DrawPng(memDC.m_hDC, rect, 0, 0);
	}
	
	if (!m_strTitle.IsEmpty())
	{
		CRect rectTitle(rect);
		rectTitle.left += 5;
		DrawMyText(memDC.m_hDC, m_strTitle, rectTitle, m_colorTitle, DT_LEFT | DT_VCENTER | DT_SINGLELINE, DMT_BOLD);
	}
	
	return 0;
}


/****************   function   *****************************/

void CXTitlebar::AddButton(UINT nID, const CString & strPng, int nButtonState, int nRightOffset, CString strText, CString strTooltipText)
{
	int nIndex = 0;
	for (nIndex = 0; nIndex < DEFAULT_BUTTON_COUNT; ++nIndex)
	{
		if (!m_pButtons[nIndex])
		{
			break;
		}
	}
	if (nIndex == DEFAULT_BUTTON_COUNT)
	{
		return;
	}

	CWindow Win = ::GetDlgItem(GetParent().m_hWnd, nID);
	m_pButtons[nIndex] = new CXPngButton();
	if (Win.m_hWnd)
	{	
		m_pButtons[nIndex]->SubclassWindow(Win.m_hWnd);
	}
	else
	{
		Win.m_hWnd = m_pButtons[nIndex]->Create(m_hWnd);
		m_pButtons[nIndex]->SetDlgCtrlID(nID);
	}
	m_pButtons[nIndex]->SetButtonPng(strPng, nButtonState);

	CRect rect = m_pButtons[nIndex]->GetRect();
	GetParent().ScreenToClient(&rect);
	if (nIndex)
	{
		rect.OffsetRect(m_RectOffset.left - rect.right + nRightOffset, 0);
	}
	else
	{
		CRect rectTitle;
		GetWindowRect(&rectTitle);
		GetParent().ScreenToClient(&rectTitle);
		int cx = rect.Width();
		rect.right = rectTitle.right - 5;
		rect.left = rect.right - cx;
	}
	
	::MoveWindow(Win.m_hWnd, rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
	m_RectOffset = rect;
}

void CXTitlebar::SetIconPng(const CString & strPng)
{
	if (!::PathFileExists(strPng))
	{
		return;
	}

	if (m_pIconPng != NULL)
	{
		DeletePngObject(m_pIconPng);
		m_pIconPng = NULL;
	}

	m_pIconPng = NewPngObject();
	if (m_pIconPng != NULL)
	{
		m_pIconPng->LoadPngFile(strPng);
	}

}